define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'department/article/index',
                    add_url: 'department/article/add',
                    edit_url: 'department/article/edit',
                    del_url: 'department/article/del',
                    detail_url: 'department/article/detail',
                    // multi_url: 'department/article/multi',
                    articleuser_url: 'department/article/articleuser',
                    banduser_url: 'department/article/banduser',
                    articlefile_url: 'my/article_file/index',
                    addcollect_url: 'my/article/addcollect',
                    publish_url: 'department/article/publish',
                    offshelf_url: 'department/article/offshelf',//下架
                    table: 'department',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'title', title: __('名称'), operate: 'LIKE', align: 'left', sortable: true,
                            formatter: function (value, row, index, field) {
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.content + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "300px"
                                    }
                                }
                            },
                        },
                        {field: 'first_author', title: __('负责人'), sortable: true, operate: 'LIKE', formatter: Table.api.formatter.label},
                        {field: 'department_name', title: __('部门'), formatter: Table.api.formatter.content, operate: false},
                        {
                            field: 'department_id',
                            title: __('部门'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="department/index/selectpage" data-field="name"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },
                        {field: 'source', title: __('来源'), operate: 'LIKE', sortable: true},
                        {field: 'direction', title: __('方向'), operate: 'LIKE', sortable: true},
                        {field: 'territory', title: __('领域'), operate: 'LIKE', sortable: true},
                        {field: 'publish', title: __("发布"), sortable: true, searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.status, operate: false},
                        {field: 'type.name', title: __('密级'), operate: 'LIKE', formatter: Table.api.formatter.label},
                        // {field: 'id', title: __('Id'), operate: false},
                        // {field: 'createuser.nickname', title: __('创建人'), operate: 'LIKE',},
                        // {field: 'position.name', title: __('职务'), operate: false, formatter: Table.api.formatter.label},
                        // {field: 'grouping.name', title: __('小组'), operate: 'LIKE', formatter: Table.api.formatter.label},
                        // {field: 'author', title: __('作者'), operate: false, formatter: Table.api.formatter.label},
                        // {field: 'introduction', title: __('简介'), operate: "LIKE", hidden: true},
                        {field: 'create_time', title: __('Createtime'), width: "120px", operate: false, formatter: Table.api.formatter.datetime, addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), width: "200px", table: table, events: Table.api.events.operate,buttons: [
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                },
                                {
                                    name: 'articleuser',
                                    text: __('添加成员'),
                                    title: __('添加成员'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.articleuser_url,
                                    hidden: function (row) {
                                        console.log(row.selfIsAuth)
                                        if (row.selfIsAuth) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    }
                                },
                                // {
                                //     name: 'banduser',
                                //     text: __('项目成员'),
                                //     title: __('项目成员'),
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     url: $.fn.bootstrapTable.defaults.extend.banduser_url,
                                // },
                                // {
                                //     name: 'addcollect',
                                //     text: __('添加收藏'),
                                //     title: __('添加收藏'),
                                //     confirm: '确认添加到我的收藏？',
                                //     classname: 'btn btn-xs btn-primary btn-ajax',
                                //     url: $.fn.bootstrapTable.defaults.extend.addcollect_url,
                                // },
                                {
                                    name: 'articlefile',
                                    text: __('附件'),
                                    title: __('附件'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.articlefile_url + "?article_id=" + row.id;
                                    },
                                },
                                {
                                    name: 'publish',
                                    text: __('发布'),
                                    title: __('发布'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.publish_url,
                                    hidden: function (row) {
                                        if (row.selfIsAuth) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    visible: function (row) {
                                        if (row.publish == 1) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                },
                                {
                                    name: 'offshelf',
                                    text: __('下架'),
                                    title: __('下架'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    confirm: "下架后不可撤销，确定下架该项目吗？",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    },
                                    url: $.fn.bootstrapTable.defaults.extend.offshelf_url,
                                    hidden: function (row) {
                                        if (row.selfIsAuth) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    visible: function (row) {
                                        if (row.publish == 0) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        articleuser: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'department/article/articleuser?ids=' + Config.ids,
                    banded_url: 'department/article/banded?article_id=' + Config.ids,
                    unband_url: 'department/article/unband?article_id=' + Config.ids,
                    setAuth_url: 'department/article/setAuth?article_id=' + Config.ids,
                    cancelAuth_url: 'department/article/cancelAuth?article_id=' + Config.ids,
                    multi_url: 'department/article/multi',
                    table: 'article',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('用户ID'), operate: false, sortable: true},
                        {field: 'nickname', title: __('姓名'), operate: "LIKE",},
                        {field: 'department_name', title: __('部门'), formatter: Table.api.formatter.label, operate: false},
                        {field: 'position.name', title: __('职务'), formatter: Table.api.formatter.label, operate: false},
                        {
                            field: 'department_id',
                            title: __('部门'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="department/index/selectpage" data-field="name"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },
                        {field: 'is_auth', title: __("管理员"), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.status, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'banded',
                                    text: __('绑定'),
                                    title: __('绑定'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    hidden: function (row) {
                                        if (row.is_band == 0) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    confirm: "确定绑定该成员吗？",
                                    url: $.fn.bootstrapTable.defaults.extend.banded_url,
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'unband',
                                    text: __('取消绑定'),
                                    title: __('取消绑定'),
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    confirm: "确定取消该成员吗？",
                                    url: $.fn.bootstrapTable.defaults.extend.unband_url,
                                    hidden: function (row) {
                                        if (row.is_band == 1) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'setAuth',
                                    text: __('设置为管理'),
                                    title: __('设置为管理'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    confirm: "确定设置该成员为管理吗？",
                                    url: $.fn.bootstrapTable.defaults.extend.setAuth_url,
                                    visible: function (row) {
                                        if (Config.selfIsAuth == 1 && row.is_auth == 0) {
                                            return true;
                                        }
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'cancelAuth',
                                    text: __('取消管理'),
                                    title: __('取消管理'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    confirm: "确定取消该成员的管理吗？",
                                    url: $.fn.bootstrapTable.defaults.extend.cancelAuth_url,
                                    visible: function (row) {
                                        if (Config.selfIsAuth == 1 && row.is_auth == 1) {
                                            return true;
                                        }
                                        return false;
                                    },
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-unband", function(){
                Layer.confirm(__('确认解绑这些人吗？'), {
                    title: __('提示'),
                    btn: [__('确认'), __('取消')]
                }, function (index) {
                    let ids = Table.api.selectedids(table);
                    ids = ids.join(",");
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.unband_url,
                        data: {ids: ids},
                        loading: true
                    }, function (data, ret) {
                        table.bootstrapTable('refresh', {});
                        Toastr.success("操作成功");
                        return false;
                    }, function (data, ret) {
                        Toastr.error('操作失败');
                        return false;
                    });
                    layer.close(index);
                }, function (index) {
                    layer.close(index);
                });

            });

        },
        banduser: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'department/article/banduser?ids=' + Config.ids,
                    dragsort_url: 'department/article/weigh?article_id=' + Config.ids,
                    table: 'article_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('ID'), sortable: true},
                        // {field: 'user.id', title: __('用户ID'), sortable: true},
                        {field: 'user.nickname', title: __('姓名'), operate: "LIKE",},
                        // {field: 'user.nickname', title: __('Nickname'), operate: "LIKE",},
                        {field: 'is_auth', title: __("管理员"), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.status, operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            visible: Config.selfIsAuth,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        publish: function () {
            Controller.api.bindevent();
        },
        excel:function ()  {
            Form.api.bindevent($("form[role=form]"));
        },
        articleuserexcel:function ()  {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});